#include <stdint.h>
#include <stdfunc.h>
#include <memory.h>
#include <multiboot.h>
#include <text_video.h>

void parse_multiboot_info(struct multiboot_info * mbi, unsigned int magic)
{
    uint32_t i;
    struct multiboot_memory_map * mbmm;
    struct multiboot_drive * mbd;
    
    if(magic != MULTIBOOT_HEADER_MAGIC)
    {
        printf("the multiboot magic value is invalid (%x), aborting...\n", magic);
        abort();
    }
    
    printf(" == MULTIBOOT INFO == \n");
    printf("BootLoader: \"%s\"\n", (char*)mbi->bootloader_name);
    printf("RAM: %u.%uMB\n", ((1024 + mbi->mem_upper) / 1024), ((1024 + mbi->mem_upper) % 1024));
    USABLE_RAM_LOW = 0x100000;
    USABLE_RAM_HIGH = 1024 + mbi->mem_upper;
        
    if(mbi->flags & (1<<6))
    {
        printf("\n == MULTIBOOT MEMORY MAP == \n");
        for(i = 0; i < mbi->mmap_length; i += (mbmm->size + 4))
        {
            mbmm = (struct multiboot_memory_map*)(mbi->mmap_address + i);

            printf("%x-%x is %s \n", mbmm->base_address_low, mbmm->base_address_low + mbmm->length_low, mbmm_types[mbmm->type]);
            
            if(mbmm->type == 1)
            {set_usable_memory(mbmm->base_address_low, mbmm->length_low);}
        }
    }
    
    if(mbi->flags & (1<<8) && *(uint16_t*)(mbi->config_table + 0x00))
    {
        printf("\n == MULTIBOOT CONFIG TABLE == \n");
        
        printf("model = %x, sub-model = %x, revision = %x\n", *(uint8_t*)(mbi->config_table + 0x02), 
                                                              *(uint8_t*)(mbi->config_table + 0x03),
                                                              *(uint8_t*)(mbi->config_table + 0x04));
        
        /* detect Phoenix BIOS */
        if(*(char*)(mbi->config_table + 0x0D) == 'P' &&
           *(char*)(mbi->config_table + 0x0E) == 'T' &&
           *(char*)(mbi->config_table + 0x0F) == 'L' &&
           *(char*)(mbi->config_table + 0x10) == 0x00)
        {printf("BIOS: Phoenix Technologies Ltd v%u.%m\n", *(uint8_t*)(mbi->config_table + 0x0B), *(uint8_t*)(mbi->config_table + 0x0C));}
        
        if(*(uint8_t*)(mbi->config_table + 0x05) & (1<<5))
        {printf("Real-Time Clock detected\n");}
        else{printf("Real-Time Clock was not detected\n");}
        
        if(*(uint8_t*)(mbi->config_table + 0x06) & (1<<2))
        {printf("non-8042 keyboard controller detected\n");}
        else{printf("8042 keyboard controller detected\n");}
        
        if(*(uint8_t*)(mbi->config_table + 0x06) & (1<<7))
        {printf("32-bit DMA support detected\n");}
        else{printf("32-bit DMA support was not detected\n");}
    }
    
    grub_global_mbi = mbi;
    
    printf("\n");
}

